package vcp.client.GUI.mvc.model;

/**
 * 
 */
import java.io.Serializable;
import java.util.List;

import vcp.client.forms.SubscriptionCancelForm;
import vcp.client.interfaces.IObservable;
import vcp.client.request.CancelSubscriptionFormRequest;
import vcp.entity.Customer;
import vcp.entity.Subscription;
import vcp.enums.PopUpMessageType;

/**
 * 
 * @author VCP team
 *         <p>
 *         Account customer status model
 *         </p>
 * @see UIAbstractModel
 */
public class UIAccStatusModel extends UIAbstractModel
{
	/** customer instance **/
	private Customer customer;

	public Customer getCustomer()
	{
		return this.customer;
	}

	public void setCustomer(Customer customer)
	{
		this.customer = customer;
	}

	public UIAccStatusModel(IObservable model)
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * cancel subscription form
	 * 
	 * @param form
	 */
	public void cancelSubscription(SubscriptionCancelForm form)
	{
		CancelSubscriptionFormRequest request = new CancelSubscriptionFormRequest(form);
		this.sendRequest(request, true);
	}

	/**
	 * find subscription by id
	 * 
	 * @param id
	 * @return subscription
	 */
	public Subscription getRequestedSubscription(Integer id)
	{
		Subscription founded = null;

		for (Subscription customerSubscription : this.customer.getSubscriptions())
		{
			if (customerSubscription.getSubscribtionID() == id)
			{
				founded = customerSubscription;
				break;
			}
		}

		return founded;
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * 
	 * @param customerInfo
	 *            <p>
	 *            send to user notification about cahnges
	 *            </p>
	 * 
	 */
	private void notifyUserOnChange(String customerInfo)
	{
		this.controllerDelegate.showPopUpMessage(customerInfo, PopUpMessageType.INFORMATION);
	}

	/**
	 * update customer balance
	 * 
	 * @param newBalance
	 */
	private void updateCustomerBalanace(Float newBalance)
	{
		this.customer.setBalance(newBalance);
	}

	/**
	 * add new subscription
	 * 
	 * @param subscription
	 */
	private void addSubscriptions(List<Subscription> subscription)
	{
		this.customer.setSubscriptions(subscription);
	}

	/**
	 * remove subscription that canceled
	 * 
	 * @param subscription
	 */
	private void removeCanceledSubscription(Subscription subscription)
	{
		Subscription customerSavedSubscription = this.getRequestedSubscription(subscription.getSubscribtionID());
		if (subscription.isDeleted())
		{

			this.customer.getSubscriptions().remove(customerSavedSubscription);
		}
		else
		{
			customerSavedSubscription.setCars(subscription.getCars());
		}
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/
	@Override
	public void clearModel()
	{
		this.customer = null;
	}

	@Override
	public void shouldRegisterForUpdates()
	{
		this.model.registerForUpdates(this);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Object data)
	{
		if (data instanceof List<?>)
		{
			List<Serializable> serializableInfo = (List<Serializable>) data;

			this.removeCanceledSubscription((Subscription) serializableInfo.get(0));
			this.updateCustomerBalanace((Float) serializableInfo.get(1));
			this.notifyUserOnChange((String) serializableInfo.get(2));

			this.controllerDelegate.updateControllerDelegateWithData(null);
			this.controllerDelegate.loadingStop();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateWithReferesh(Object data)
	{
		if (data instanceof List<?>)
		{
			List<Serializable> serializableInfo = (List<Serializable>) data;

			this.addSubscriptions((List<Subscription>) serializableInfo.get(0));
			this.updateCustomerBalanace((Float) serializableInfo.get(1));

			this.controllerDelegate.updateControllerDelegateWithData(null);
			this.controllerDelegate.loadingStop();
		}
	}
}
